const app = getApp()
var utils = require("../../utils/util");
Page({
  data: {
    debug: false, //判断是否为本地测试，这个要手动设置，是否用本地环境
    chooseType: "1", //01为测试腾讯云，02为测试百度云，1为线上腾讯，2为线上百度，通过页面单选按钮切换
    resStr: "暂无结果", //识别结果
    gifSrc: "",
    loadingArr: ["../../assets/11.png", "../../assets/22.png", "../../assets/33.png", "../../assets/44.png"],
    completeArr: ["../../assets/1.png", "../../assets/2.png", "../../assets/3.png", "../../assets/4.png"],

  },
  //选择技术实现
  radioChange(e) {
    if (e.detail.value == 1) { //选择腾讯云
      if (this.data.debug) {
        this.setData({
          chooseType: "01"
        })
      } else {
        this.setData({
          chooseType: "1"
        })
      }
    }
    if (e.detail.value == 2) { //选择百度云
      if (this.data.debug) {
        this.setData({
          chooseType: "02"
        })
      } else {
        this.setData({
          chooseType: "2"
        })
      }
    }
  },

  // 拍照
  takePhoto() {
    let nowTime = Math.floor(new Date().getTime() / 1000);
    let that = this;
    //这个showToast和下面的播放loading动画二选一
    wx.showToast({
      title: '识别中',
      mask:true,
      icon:"none"
    })
    //that.gifStart(that.data.loadingArr); //开始播放loading
    let requestUrl = "";
    switch (that.data.chooseType) {
      case "01":
        requestUrl = "http://www.apiTest.com/tencentOcrApi/sample.php";
        break;
      case "02":
        requestUrl = "http://www.apiTest.com/baiduOcrApi/shuaiSample.php";
        break;
      case "1":
        requestUrl = "https://ar.bluej.cn/tencentOcrApi/sample.php";
        break;
      case "2":
        requestUrl = "https://ar.bluej.cn/baiduOcrApi/shuaiSample.php";
        break;
    }
    const ctx = wx.createCameraContext();
    console.log("请求地址", requestUrl);
    ctx.takePhoto({
      quality: 'low',
      success: (res) => {
        console.log("图片生成地址", res.tempImagePath);
        try {
          wx.uploadFile({
            url: requestUrl,
            filePath: res.tempImagePath,
            name: 'pic',
            success: function (res) {
              wx.hideToast();
              let result = false;
              let resStr = "";
              if (["01", "1"].indexOf(that.data.chooseType) != -1) { //腾讯云
                var data = JSON.parse(res.data);
                data = JSON.parse(data);
                console.log(data);
                data.data.items.forEach((item, key) => {
                  resStr += item.itemstring + "\n";
                  resStr = utils.myTrim(resStr);
                  !result && (result = that.hasTargetChar(utils.myTrim(item.itemstring)));
                })
              }
              if (["02", "2"].indexOf(that.data.chooseType) != -1) { //百度云
                let data = JSON.parse(res.data);
                console.log(data);
                data.words_result.forEach((cur, index) => {
                  resStr += cur.words + "\n";
                  !result && (result = that.hasTargetChar(utils.myTrim(cur.words)));
                })
              }
              that.setData({
                resStr,
              });
              wx.showToast({
                icon: "none",
                title: result ? "恭喜你，扫到了！" : "很遗憾，没扫到！",
                duration: 4000,
                mask: false
              })
              if (result) {
                that.gifStart(that.data.completeArr);
                setTimeout(() => {
                  that.gifStop();
                }, 3800)
              } else {
                that.gifStop();
              }
            }
          })
        } catch (e) {
          wx.showToast({
            icon: "none",
            title: "错误",
          })
          console.log(e);
        }
      }
    })
  },
  // 检测是否包含给定文字
  hasTargetChar(toCheckedStr) {
    let targetArr = ["福", "一口", "一田", "口田", "不", "礻", "富", "匐", "鶝", "s", "S", "e", "逼", "畐", "回", "元", "国", "T", "G", "由", "昌", "娼", "锠", "倡"];
    // let targetArr = ["邮", "由", "1|", "邠", "耶", "阝", "邯", "口阝", "口阝", "回", "却", "tp", "Ep", "E", "11", "1l", '1ι', '1┃',"1丨"]
    for (let i = 0; i < targetArr.length; i++) {
      if (toCheckedStr.indexOf(targetArr[i]) != -1) {
        return true;
      }
    }
    return false;
  },
  //gif效果
  gifStart(gifArr = []) {
    let i = 0;
    let that = this;
    if (that.timer == 0 || that.timer) {
      clearInterval(that.timer);
    }
    that.timer = setInterval(() => {
      i++;
      if (i >= gifArr.length) {
        i = 0
      };
      that.setData({
        gifSrc: gifArr[i]
      })
    }, 100)
  },
  gifStop() {
    if (this.timer == 0 || this.timer) {
      clearInterval(this.timer);
    }
    this.setData({
      gifSrc: ""
    })
  }
})